package com.example.dangerouscargodriver.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.entry.ResourceInfo;
import com.example.dangerouscargodriver.entry.ResourceStatusInfo;
import com.example.dangerouscargodriver.ui.activity.MyResourceDetailActivity;
import com.example.dangerouscargodriver.ui.activity.ResourceDetailActivity;

public class MyResourceInfoView extends LinearLayout{

    private static final String TAG = "ResourceInfoView";
    private TextView mFromName = null;
    private TextView mToName = null;
    private TextView mSourceDistance = null;
    private TextView mPublishTime = null;
    private TextView mPublisher = null;
    private TextView mPublishState = null;
    private TextView mRegTime = null;
    private TextView mLoadTime = null;
    private TextView mLoadTimeTit = null;
    private Button mGrapOrder = null;
    private LinearLayout mPublishTimeLay;
    private LinearLayout mMetaLayout = null;
    private Context mContext = null;
    private String mResourceType = null;

    private ResourceStatusInfo mResourceInfo = null;

    public MyResourceInfoView(Context context, ResourceStatusInfo info) {
        this(context, null, info);
    }

    public MyResourceInfoView(Context context, AttributeSet attrs, ResourceStatusInfo info) {
        this(context, attrs, 0, info);
    }

    public MyResourceInfoView(Context context, AttributeSet attrs, int defStyleAttr, ResourceStatusInfo info) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, info);
    }

    public void init(Context context, AttributeSet attrs, ResourceStatusInfo info) {
        mContext = context;
        mResourceInfo = info;
        View itemLayout = LayoutInflater.from(context).inflate(R.layout.layout_resource_info_item, this);
        mFromName = itemLayout.findViewById(R.id.item_start_city);
        mSourceDistance = itemLayout.findViewById(R.id.item_source_distance);
        mToName = itemLayout.findViewById(R.id.item_end_city);
        mPublishTime = itemLayout.findViewById(R.id.tv_publish_time);
        mPublisher = itemLayout.findViewById(R.id.tv_resource_publisher);
        mPublishState = itemLayout.findViewById(R.id.tv_order_state);
        mRegTime = itemLayout.findViewById(R.id.item_register_time);
        mGrapOrder = itemLayout.findViewById(R.id.btn_grab_order);
        mPublishTimeLay = itemLayout.findViewById(R.id.lay_publish_time);
        mMetaLayout = itemLayout.findViewById(R.id.layout_resource_meta);
        mLoadTime = itemLayout.findViewById(R.id.tv_load_time);
        mLoadTimeTit = itemLayout.findViewById(R.id.tv_load_time_title);

        mRegTime.setVisibility(View.GONE);
        mGrapOrder.setVisibility(View.GONE);
        mPublishState.setVisibility(View.VISIBLE);
        mPublisher.setVisibility(View.VISIBLE);
        mPublishTimeLay.setVisibility(View.VISIBLE);

        mLoadTimeTit.setTextColor(Color.GRAY);
        mLoadTime.setTextColor(Color.GRAY);

        itemLayout.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MyResourceDetailActivity.class);
                intent.putExtra("recid", mResourceInfo.getRecid());
                mContext.startActivity(intent);
            }
        });
    }

    public void bindData(ResourceStatusInfo info){
        if(info != null){
            mFromName.setText(mResourceInfo.getFrom_name());
            mToName.setText(mResourceInfo.getTo_name());
            mSourceDistance.setText(mResourceInfo.getSource_distance() + "km");
            mPublishTime.setText(mResourceInfo.getRegtime());

            String loadTime = "";
            if(mResourceInfo.getLoadtime2() == null || mResourceInfo.getLoadtime().equals(mResourceInfo.getLoadtime2())){
                loadTime = mResourceInfo.getLoadtime();
            }
            else{
                loadTime = mResourceInfo.getLoadtime() + "~" + mResourceInfo.getLoadtime2();
            }

            mLoadTime.setText(loadTime);
            mPublishState.setText(mResourceInfo.getStatus_name());

            int stateColor = Color.BLACK;
            switch (mResourceInfo.getStatus()){
                case "1":
                    stateColor = Color.rgb(255,165,0);
                    break;
                case "2":
                    stateColor = R.color.orange;
                    break;
                case "3":
                    stateColor = Color.BLUE;
                    break;
                case "4":
                    stateColor = Color.BLACK;
                    break;
                case "5":
                    stateColor = Color.RED;
                    break;
                case "6":
                    stateColor = Color.RED;
                    break;
            }

            mPublishState.setTextColor(stateColor);
            mPublisher.setText(mResourceInfo.getContact() + "(" + mResourceInfo.getMobilephone() + ")");

            if(mResourceInfo.getName() != null && !mResourceInfo.getName().equals("")){
                View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_resource_meta_info_item, null);
                TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                textView.setText(mResourceInfo.getName());
                mMetaLayout.addView(layout);
            }
            if(mResourceInfo.getClassname() != null && !mResourceInfo.getClassname().equals("")){
                View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_resource_meta_info_item, null);
                TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                textView.setText(mResourceInfo.getClassname());
                mMetaLayout.addView(layout);
            }
            if(mResourceInfo.getWeight() != null && !mResourceInfo.getWeight().equals("")){
                View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_resource_meta_info_item, null);
                TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                textView.setText(mResourceInfo.getWeight() + "吨");
                mMetaLayout.addView(layout);
            }
            if(mResourceInfo.getTruck_type() != null && !mResourceInfo.getTruck_type().equals("")){
                String truckType = mResourceInfo.getTruck_type();
                Log.d(TAG, "truckType: " + truckType);
                String[] truckTypeArray = truckType.split("、");
                Log.d(TAG, "truckTypeArray: " + truckTypeArray.toString());

                for(int index = 0; index < truckTypeArray.length; index++){
                    View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_resource_meta_info_item, null);
                    TextView textView = layout.findViewById(R.id.tv_item_meta_string);
                    textView.setText(truckTypeArray[index]);
                    mMetaLayout.addView(layout);
                }
            }
        }
    }

    public void bindType(String type){
        mResourceType = type;
    }
}
